var user;
apiready = function() {





	$api.getUserInf(function(userInf) {
		if (!userInf) {
			api.closeWin();
			$api.toastMsg("请先绑定业主信息");
			return;
		}
		user = userInf;


		$(".order_tab").find('a').click(function() {

			$(".order_tab").find('a').removeClass('aon');
			$(this).addClass('aon');
			loadMyOrder();
			return false;
		});
		$("#orderType").change(function() {
			loadMyOrder();
		});
		loadMyOrder();
	});
};

function openGroupWin(groupId) {
	$api.mOpenWin('groupInf', 'group_info.html', {
		id: groupId
	});
}

function openOrderWin(orderNo) {
	$api.mOpenWin('orderInf', 'fight_group.html', {
		orderNo: orderNo
	});
}

function openOrderInf(orderId){
	$api.mOpenWin('orderInf', 'order_info.html', {
		orderId: orderId
	});
}

var loadMyOrder = function(pageIndex) {
	api.showProgress();
	if (!pageIndex) {
		pageIndex = 1;
	}
	var getUrl = '/wx/pgroup/myorder/data';
	var orderType = $("#orderType").val();
	var status = $(".order_tab").find('.aon').text();
	if (status == '全部')
		status = '';
	var data = {
		dd: new Date().getTime(),
		rows: 20,
		page: pageIndex,
		'account': act,
		'search$$orderType$$excat': orderType,
		'search$$status$$excat': status,

	};
	if (pageIndex === 1) {
		$('#orders').html('');
	}

	$api.do_post("/api/pgroup/myorder/data", {
		body: JSON.stringify({
			account: user.account,
			rows: 20,
			page: pageIndex,
			'orderType': orderType,
			'status': status,
		})
	}, function(ret, err) {
		//console.log(json);
		var list = ret.rows;
		var zting = $("#zting").html();
		var ztfail = $("#ztfail").html();
		var order = $("#order").html();

		$.each(list, function(i, item) {
			// $api.log(item);
			//console.log(item);
			var html = "";
			if (item.orderType == '拼团') {
				console.error(item.status);
				switch (item.status) {
					case '待支付':
						$api.replaceFileHtml('_orderGroup.html', item, function(re) {
							$('#orders').append(re);
							html = re;
						});
						break;
					case '待成团':
						$api.replaceFileHtml('_orderGroupIng.html', item, function(re) {
							$('#orders').append(re);
							html = re;
						});
						break;
					case '作废':
						$api.replaceFileHtml('_orderGroupIng.html', item, function(re) {
							$('#orders').append(re);
						});
						break;
					default:
						$api.replaceFileHtml('_orderGroupIng.html', item, function(re) {
							$('#orders').append(re);
						});
						html = order;
						break;
				}
			} else {
				$api.replaceFileHtml('_orderNormalPay.html', item, function(re) {
					$('#orders').append(re);
				});
			}


			// for (var key in item) {
			// 	var regex = new RegExp('{{' + key + '}}', 'g');
			// 	html = html.replace(regex, item[key]);
			// }


		});
	});
};